package com.dearbinge.data.service.impl.userSolicitude;

import com.baomidou.mybatisplus.plugins.Page;
import com.dearbinge.data.api.userSolicitude.UserSolicitudeService;
import com.dearbinge.data.dao.UserSolicitudeMapper;
import com.dearbinge.data.pojo.userSolicitude.UserSolicitude;
import com.dearbinge.utils.Base64Util;
import com.dearbinge.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service(value = "userSolicitudeService")
public class UserSolicitudeServiceImpl implements UserSolicitudeService {

    @Autowired
    private UserSolicitudeMapper userSolicitudeMapper;

    @Override
    public Integer update(UserSolicitude userSolicitude) {
        Integer dataCode = userSolicitudeMapper.update(userSolicitude);
        return dataCode;
    }

    @Override
    public Integer insert(UserSolicitude userSolicitude) {
        Integer dataCode = userSolicitudeMapper.insert(userSolicitude);
        return dataCode;
    }

    @Override
    public Integer delete(Integer id) {
        Integer dataCode = userSolicitudeMapper.delete(id);
        return dataCode;
    }

    @Override
    public UserSolicitude getById(Integer id) {
        UserSolicitude userSolicitude = userSolicitudeMapper.getById(id);
        return userSolicitude;
    }

    @Override
    public PageResult<List<Map<String,Object>>> getByUserId(String userId, Integer type, Integer pageNo, Integer pageSize) {
        pageNo = (null == pageNo || pageNo < 1) ? 1 : pageNo;
        pageSize = (null == pageSize || pageSize < 0) ? 10 :pageSize;
        Page page = new Page(pageNo,pageSize);
        Integer count = 0;
        List<Map<String,Object>> userSolicitude = null;
        if (type != null && type == 0){
            count = userSolicitudeMapper.count(userId);
            userSolicitude =userSolicitudeMapper.getByUserId(userId,page);
        }else if (type == 1){
            count = userSolicitudeMapper.getCountBeConcerned(Integer.parseInt(userId));
            userSolicitude =userSolicitudeMapper.getBeConcerned(userId,page);
            if (userSolicitude != null)for(int index = 0;index < userSolicitude.size();index++){
                Map<String, Object> map = userSolicitude.get(index);
                String userId1 = map.get("userId").toString();
                String solicitudeId = map.get("solicitudeId").toString();
                String isSolicitude = userSolicitudeMapper.getIsSolicitude(solicitudeId, userId1);
                userSolicitude.get(index).put("isSolicitude", StringUtils.isEmpty(isSolicitude) ? 0 :1);
            }
        }
        if (!StringUtils.isEmpty(userSolicitude))for (Map<String, Object> map : userSolicitude) {
                map.put("uName", map.get("uName") == null ? null : Base64Util.decodeData(map.get("uName").toString()) );
            }
        Integer max = count % pageSize == 0 ? count / pageSize : count / pageSize +1;
        PageResult<List<Map<String,Object>>> pageResult = new PageResult<List<Map<String,Object>>>(userSolicitude);
        pageResult.setMaxPageSize(max);
        return pageResult;
    }

    @Override
    public String getIsSolicitude(String userId, String solicitudeId) {
        String id = userSolicitudeMapper.getIsSolicitude(userId,solicitudeId);
        return id;
    }

    @Override
    public Integer cancelSolicitude(String userId, String solicitudeId) {
        Integer dataCode = userSolicitudeMapper.cancelSolicitude(userId,solicitudeId);
        return dataCode;
    }

    @Override
    public Integer getByUserIdAndSolicitudeId(String userId, String solicitudeId) {
        Integer id = userSolicitudeMapper.getByUserIdAndSolicitudeId(userId,solicitudeId);
        return id;
    }

    @Override
    public Integer getCountBeConcerned(Integer id) {
        Integer fans = userSolicitudeMapper.getCountBeConcerned(id);
        return fans;
    }

    @Override
    public Integer count(Integer id) {
        Integer count = userSolicitudeMapper.count(id.toString());
        return count;
    }
}
